@ECHO OFF

SETLOCAL ENABLEEXTENSIONS

SET TOPDIR=%~dp0
SET PATH=%TOPDIR%\tools\GNUmake;%TOPDIR%\tools\gcc-arm-none-eabi\bin;%PATH%

:: echo ******  CHIP: %CHIP%
:: echo ******  MODULE: %MODULE%
:: echo ******  SOURCE: %SOURCE%


SET CHIPLIST=1602c,1602s,1606l,1606c,1606s
:: SET MODULELIST=HV9,HE9,HE9EA,HE9EU,HE9NA,HV9V2,HV9G,HV9B,CV9,MV9,LV9
SET MODULELIST=HV9,HE9EA,HE9EU,HE9NA,HV9V2,HV9G,HV9B,CV9,MV9,LV9

SET argC=0
FOR %%i IN (%*) DO (
	SET /A argC+=1
)

SET CHIP=
SET MODULE=
SET SOURCE=
SET CLEAN=

:: @ECHO %argC%

IF "%argC%"=="3" (
	IF not "%1"=="-c" GOTO USAGE
	SET CHIP=%2
	SET SOURCE=%3
) ELSE IF "%argC%"=="4" (
	IF not "%1"=="-c" GOTO USAGE
	SET CHIP=%2
	SET SOURCE=%3
	IF "%4"=="clean" (
		SET CLEAN=clean
	) ELSE (
		@ECHO ---------------------------------------
		@ECHO -----The last build param is error!
		@ECHO ---------------------------------------
		GOTO USAGE
	)
) ELSE IF "%argC%"=="5" (
	IF not "%1"=="-c" GOTO USAGE
	SET CHIP=%2
	IF not "%3"=="-autopkg" GOTO USAGE
	SET MODULE=%4
	SET SOURCE=%5
) ELSE IF "%argC%"=="6" (
	IF not "%1"=="-c" GOTO USAGE
	SET CHIP=%2
	IF not "%3"=="-autopkg" GOTO USAGE
	SET MODULE=%4
	SET SOURCE=%5
	IF "%6"=="clean" (
		SET CLEAN=clean
	) ELSE (
		@ECHO ---------------------------------------
		@ECHO -----The last build param is error!
		@ECHO ---------------------------------------
		GOTO USAGE
	)
) ELSE (
	@ECHO ---------------------------------------
	@ECHO -----The build param is error!
	@ECHO ---------------------------------------
	GOTO USAGE
)

:: check the chip
FOR %%i IN (%CHIPLIST%) DO (
	if "%%i"=="%CHIP%" (
		GOTO CHIP_MATACH
	)
)

@ECHO ---------------------------------------
@ECHO -----Chip type is error %CHIP%!
@ECHO ---------------------------------------
GOTO USAGE
:CHIP_MATACH

IF not "%MODULE%"=="" (
	FOR %%i IN (%MODULELIST%) DO (
		if "%%i"=="%MODULE%" (
			GOTO MODULE_MATACH
		)
	)
) ELSE (
	GOTO SOURCE_CHECK
)
@ECHO ---------------------------------------
@ECHO -----Module name is error!
@ECHO ---------------------------------------
GOTO USAGE
:MODULE_MATACH


:SOURCE_CHECK
IF "%SOURCE%" == "" (
	GOTO USAGE
)

IF "%CLEAN%" == "clean" (
	make -C %SOURCE% clean
	rmdir /s /q out
	GOTO END
)


if not exist out\ (
	mkdir out
)
del /Q  out\app.*

make -C %SOURCE%

set MAKE_ERRORLEVEL=%ERRORLEVEL%

if not %MAKE_ERRORLEVEL% == 0 (
	GOTO END
)

IF "%MODULE%" == "" (
	GOTO END
)

@ECHO -----build SUCCESS!

:SUCCESS

set BUILD_DATE=%date:~0,4%%date:~5,2%%date:~8,2%
set BUILD_TIME=%time:~0,2%%time:~3,2%%time:~6,2%
set BUILD_TIME=%BUILD_TIME: =0%

::echo %BUILD_DATE%_%BUILD_TIME%
::echo ******  CHIP: %CHIP%
::echo ******  MODULE: %MODULE%

copy out\app.bin tools\aboot\images

SET FLASH_SIZE=
IF "%CHIP%" == "1602c" (
	SET FLASH_SIZE=02MB
	GOTO ASR1602_PACKAGE_FW
) ELSE IF "%CHIP%" == "1602s" (
    SET FLASH_SIZE=04MB
	GOTO ASR1602_PACKAGE_FW
) ELSE IF "%CHIP%" == "1606l" (
	SET FLASH_SIZE=02MB
	GOTO ASR1606_PACKAGE_FW
) ELSE IF "%CHIP%" == "1606c" (
    SET FLASH_SIZE=04MB
	GOTO ASR1606_PACKAGE_FW
) ELSE IF "%CHIP%" == "1606s" (
    SET FLASH_SIZE=08MB
	GOTO ASR1606_PACKAGE_FW
) ELSE (
	@ECHO ---------------------------------------
	@ECHO -----Chip type error, can not package firmware!
	@ECHO ---------------------------------------
	GOTO END
)

:ASR1602_PACKAGE_FW
IF "%MODULE%" == "HV9" (
	call tools\aboot\arelease.exe -c tools\aboot -g -p YUGE_1602 -v HV9_%FLASH_SIZE% out\HV9_%FLASH_SIZE%_%BUILD_DATE%_%BUILD_TIME%.zip
) ELSE IF "%MODULE%" == "HE9" (
	call tools\aboot\arelease.exe -c tools\aboot -g -p YUGE_1602 -v HE9_%FLASH_SIZE% out\HE9_%FLASH_SIZE%_%BUILD_DATE%_%BUILD_TIME%.zip
) ELSE IF "%MODULE%" == "HE9EA" (
	call tools\aboot\arelease.exe -c tools\aboot -g -p YUGE_1602 -v HE9EA_%FLASH_SIZE% out\HE9EA_%FLASH_SIZE%_%BUILD_DATE%_%BUILD_TIME%.zip
) ELSE IF "%MODULE%" == "HE9EU" (
	call tools\aboot\arelease.exe -c tools\aboot -g -p YUGE_1602 -v HE9EU_%FLASH_SIZE% out\HE9EU_%FLASH_SIZE%_%BUILD_DATE%_%BUILD_TIME%.zip
) ELSE IF "%MODULE%" == "HE9NA" (
	call tools\aboot\arelease.exe -c tools\aboot -g -p YUGE_1602 -v HE9NA_%FLASH_SIZE% out\HE9NA_%FLASH_SIZE%_%BUILD_DATE%_%BUILD_TIME%.zip
) ELSE IF "%MODULE%" == "HV9V2" (
	call tools\aboot\arelease.exe -c tools\aboot -g -p YUGE_1602 -v HV9V2_%FLASH_SIZE% out\HV9V2_%FLASH_SIZE%_%BUILD_DATE%_%BUILD_TIME%.zip
) ELSE IF "%MODULE%" == "HV9G" (
	call tools\aboot\arelease.exe -c tools\aboot -g -p YUGE_1602 -v HV9G_%FLASH_SIZE% out\HV9G_%FLASH_SIZE%_%BUILD_DATE%_%BUILD_TIME%.zip
) ELSE IF "%MODULE%" == "HV9B" (
	call tools\aboot\arelease.exe -c tools\aboot -g -p YUGE_1602 -v HV9B_%FLASH_SIZE% out\HV9B_%FLASH_SIZE%_%BUILD_DATE%_%BUILD_TIME%.zip	
) ELSE IF "%MODULE%" == "CV9" (
	call tools\aboot\arelease.exe -c tools\aboot -g -p YUGE_1602 -v CV9_%FLASH_SIZE% out\CV9_%FLASH_SIZE%_%BUILD_DATE%_%BUILD_TIME%.zip	
) ELSE (
	@ECHO ---------------------------------------
	@ECHO -----Chip and Module not match, can not package firmware!
	@ECHO ---------------------------------------
)
GOTO END

:ASR1606_PACKAGE_FW
IF "%MODULE%" == "MV9" (
	:: call tools\aboot\arelease.exe -c tools\aboot -g -p YUGE_1606 -v MV9_08MB out\MV9_08MB_%BUILD_DATE%_%BUILD_TIME%.zip
	call tools\aboot\arelease.exe -c tools\aboot -g -p YUGE_1606 -v MV9_%FLASH_SIZE% out\MV9_%FLASH_SIZE%_%BUILD_DATE%_%BUILD_TIME%.zip
) ELSE IF "%MODULE%" == "LV9" (
	call tools\aboot\arelease.exe -c tools\aboot -g -p YUGE_1606 -v LV9_%FLASH_SIZE% out\LV9_%FLASH_SIZE%_%BUILD_DATE%_%BUILD_TIME%.zip
) ELSE (
	@ECHO ---------------------------------------
	@ECHO -----Chip and Module not match, can not package firmware!
	@ECHO ---------------------------------------
)
GOTO END

:USAGE
@ECHO ****************************************************************
@ECHO ************************USAGE: readme.md************************
@ECHO ****************************************************************
@ECHO build_1
@ECHO ---------build.bat -c 1606l sample\task
@ECHO clean_1
@ECHO ---------build.bat -c 1606l sample\task clean
@ECHO build_2
@ECHO ---------build.bat -c 1606l -autopkg MV9 sample\task
@ECHO clean_2
@ECHO ---------build.bat -c 1606l -autopkg MV9 sample\task clean
@ECHO ***
@ECHO        -c chip_type:   		1602c   // 4MB RAM + 2MB FLASH
@ECHO                        		1602s   // 4MB RAM + 4MB FLASH
@ECHO                        		1606l   // 4MB RAM + 2MB FLASH
@ECHO                        		1606c   // 4MB RAM + 4MB FLASH
@ECHO                        		1606s   // 8MB RAM + 8MB FLASH
@ECHO ***
@ECHO        -autopkg module_name:	HV9     //chip may be 1602c or 1602s
@ECHO                            	HE9EA   //chip may be 1602c or 1602s
@ECHO                            	HE9EU   //chip may be 1602c or 1602s
@ECHO                            	HE9NA   //chip may be 1602c or 1602s
@ECHO                            	HV9V2   //chip may be 1602c or 1602s
@ECHO                            	HV9G    //chip may be 1602c or 1602s
@ECHO                            	HV9B    //chip may be 1602c or 1602s
@ECHO                            	CV9     //chip may be 1602c or 1602s
@ECHO                            	MV9     //chip may be 1606l, 1606c or 1606s
@ECHO                            	LV9     //chip may be 1606l, 1606c or 1606s
@ECHO ***
@ECHO        the last param is the app code dir!
@ECHO ****************************************************************

:END